#!/bin/sh -x

# set this to yes to force level0 dump
DO_FULL_BACKUP="no"
case "$1" in
    --full-backup)
      DO_FULL_BACKUP="yes"
      ;;
esac


dump_disk () {
    # $1 = level
    # $2 = image
    # $3 = disk
    dump -D /backup/dumpdate -u -a -$1 -f $2.$1 -L $3 # >> /var/log/dump-`basename $3`.log
    RETVAL=$?
    if [ "$RETVAL" != "0" ];then
       echo "dump -D /backup/dumpdate -u -a -$1 -f $2.$1 -L $3 failed" |wall
    fi
    bzip2 $2.$1
    return $RETVAL
}

run_full_backup () {
   # $1 = disk
   # $2 = image
   if [ ! -d `dirname $1`/undo ];then
      mkdir -p `dirname $1`/undo
   fi
   mv $2.* `dirname $1`/undo/
   dump_disk 0 $1 $2
   if [ "$RETVAL" = "0" ];then
     rm -rf `dirname $1`/undo/
   fi
}

run_backup (){
   # $1 = disk
   # $2 = image
   if [ "$DO_FULL_BACKUP" = "yes" ];then
      dump_disk 0 $1 $2
   else
      TODAY=`date "+%A"`
      case "$TODAY" in 
          Monday)
             run_full_backup $1 $2
             ;;
          Tuesday)
             dump_disk 1 $1 $2
             ;;
          Wednesday)
             dump_disk 2 $1 $2
             ;;
          Thursday)
             dump_disk 3 $1 $2
             ;;
          Friday)
             dump_disk 4 $1 $2
             ;;
          Saturday)
             dump_disk 5 $1 $2
             ;;
          Sunday)
             dump_disk 6 $1 $2
             ;;
       esac
    fi
}


# run_backup <path-to-save-dump> <path-to-device>
# eg:
# run_backup /backup/mydisk-backup.dump /dev/ad0s1a

